package com.czwj.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.dao.AttachmentMapper;
import com.czwj.dao.AttachmentRelyMapper;
import com.czwj.dao.LettertextMapper;
import com.czwj.entities.Attachment;
import com.czwj.entities.AttachmentRely;
import com.czwj.entities.Lettertext;
import com.czwj.service.LettertextService;

@Service
public class LettertextServiceImpl implements LettertextService{
	
	@Resource
	LettertextMapper mapper;
	@Resource
	AttachmentMapper amapper;
	@Resource
	AttachmentRelyMapper relymapper;
	
	
	@Value("${resources.phypath}")
	private String PhyPath;

	@Value("${resources.respath}")
	private String ResPath;
	
	private final String Attachment_Mail_Path = "邮件/";
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@Override
	public List<Lettertext> selectLetterList(String recname) {
		// TODO Auto-generated method stub
		return mapper.selectLetterList(recname);
	}
	@Override
	public Lettertext selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return mapper.selectByPrimaryKey(id);
	}
	@Override
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return mapper.deleteByPrimaryKey(id);
	}
	
	@Override
	public int updateByPrimaryKeySelective(Lettertext record) {
		// TODO Auto-generated method stub
		return mapper.updateByPrimaryKeySelective(record);
	}
	@Override
	public List<Lettertext> selectByState(String recname) {
		// TODO Auto-generated method stub
		return mapper.selectByState(recname);
	}
	@Override
	@Transactional
	public int insertSelective(Lettertext record, MultipartFile[] images) {
		int count = 0;
		try {
			count += mapper.insertSelective(record);
			String path = Attachment_Mail_Path+"/"+record.getTitle()+sdf.format(record.getSenddate()) +"/";
			String sourcepath = PhyPath+path;//存储路径
			String urlpath = ResPath+path;//资源路径
			File dest = new File(sourcepath);
			if (!dest.exists()) {
				dest.mkdirs();
			}
			for (MultipartFile image : images) {
				String filename = image.getOriginalFilename();
				if(!filename.contains(".")) {
					filename+=".jpg";
				}
				String _sourcepath = sourcepath+filename;
				String _urlpath =urlpath+filename;
				File file = new File(_sourcepath);
				while (file.exists()) {
					String[] arr =  _sourcepath.split("\\.");
					_sourcepath="";
					for(int i=0;i<arr.length;i++){
						if(i<arr.length-1){
							_sourcepath += arr[i];;
						}else{
							_sourcepath+="(1)."+arr[i];
						}
					}
					file = new File(_sourcepath);
				}
				image.transferTo(new File(_sourcepath));
				Attachment attachment = new Attachment(filename, _urlpath, _sourcepath, "image", record.getSendid(), new Date(), "127.0.0.1");
				count+=this.amapper.insertSelective(attachment);
				AttachmentRely aRely = new AttachmentRely("mail", record.getId(), attachment.getId());
				count+=this.relymapper.insertSelective(aRely);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return 0;
		}
		return count;
	}
	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.LettertextService#selectSendMsg(java.lang.Integer)
	*/
	@Override
	public List<Lettertext> selectSendMsg(Integer id){
		// TODO Auto-generated method stub
		return mapper.selectSendMsg(id);
	}

}
